package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Additive;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("AdditiveDAO")
@Transactional
public class AdditiveDAOImpl extends AbstractJpaDao<Additive> implements AdditiveDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Additive.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public AdditiveDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Additive findAdditiveById(Integer id) throws DataAccessException {

		return findAdditiveById(id, -1, -1);
	}

	@Transactional
	public Additive findAdditiveById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveById", startResult, maxRows, id);
			return (Additive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Additive> findAdditiveByName(String name) throws DataAccessException {

		return findAdditiveByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additive> findAdditiveByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAdditiveByName", startResult, maxRows, name);
		return new LinkedHashSet<Additive>(query.getResultList());
	}

	@Transactional
	public Set<Additive> findAdditiveByNameContaining(String name) throws DataAccessException {

		return findAdditiveByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additive> findAdditiveByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Additive>(query.getResultList());
	}

	@Transactional
	public Set<Additive> findAdditiveByDescription(String description) throws DataAccessException {

		return findAdditiveByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additive> findAdditiveByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Additive>(query.getResultList());
	}

	@Transactional
	public Set<Additive> findAdditiveByDescriptionContaining(String description) throws DataAccessException {

		return findAdditiveByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additive> findAdditiveByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdditiveByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Additive>(query.getResultList());
	}

	@Transactional
	public Additive findAdditiveByPrimaryKey(Integer id) throws DataAccessException {
		return findAdditiveByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Additive findAdditiveByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveByPrimaryKey", startResult, maxRows, id);
			return (Additive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Additive> findAdditivesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditivesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Additive> findAllAdditives() throws DataAccessException {

		return findAllAdditives(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additive> findAllAdditives(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAdditives", startResult, maxRows);
		return new LinkedHashSet<Additive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Additive> findAdditiveByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Additive>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Additive> findAdditivesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Additive.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Additive> additives = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return additives;
	}

	@Transactional
	public Additive findAdditiveByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findAdditiveByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Additive findAdditiveByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdditiveByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Additive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Additive entity) {
		return true;
	}
}
